const fs = require('fs');
const path = require('path');

// 圣经书卷配置
const bibleBooks = {
  // 旧约全书
  oldTestament: [
    { id: 'gbGen', name: '创世记', english: 'Genesis', chapters: 50 },
    { id: 'gbExo', name: '出埃及记', english: 'Exodus', chapters: 40 },
    { id: 'gbLev', name: '利未记', english: 'Leviticus', chapters: 27 },
    { id: 'gbNum', name: '民数记', english: 'Numbers', chapters: 36 },
    { id: 'gbDeu', name: '申命记', english: 'Deuteronomy', chapters: 34 },
    { id: 'gbJos', name: '约书亚记', english: 'Joshua', chapters: 24 },
    { id: 'gbJug', name: '士师记', english: 'Judges', chapters: 21 },
    { id: 'gbRut', name: '路得记', english: 'Ruth', chapters: 4 },
    { id: 'gb1Sa', name: '撒母耳记上', english: '1 Samuel', chapters: 31 },
    { id: 'gb2Sa', name: '撒母耳记下', english: '2 Samuel', chapters: 24 },
    { id: 'gb1Ki', name: '列王记上', english: '1 Kings', chapters: 22 },
    { id: 'gb2Ki', name: '列王记下', english: '2 Kings', chapters: 25 },
    { id: 'gb1Ch', name: '历代志上', english: '1 Chronicles', chapters: 29 },
    { id: 'gb2Ch', name: '历代志下', english: '2 Chronicles', chapters: 36 },
    { id: 'gbEzr', name: '以斯拉记', english: 'Ezra', chapters: 10 },
    { id: 'gbNeh', name: '尼希米记', english: 'Nehemiah', chapters: 13 },
    { id: 'gbEst', name: '以斯帖记', english: 'Esther', chapters: 10 },
    { id: 'gbJob', name: '约伯记', english: 'Job', chapters: 42 },
    { id: 'gbPsm', name: '诗篇', english: 'Psalms', chapters: 150 },
    { id: 'gbPro', name: '箴言', english: 'Proverbs', chapters: 31 },
    { id: 'gbEcc', name: '传道书', english: 'Ecclesiastes', chapters: 12 },
    { id: 'gbSon', name: '雅歌', english: 'Song of Solomon', chapters: 8 },
    { id: 'gbIsa', name: '以赛亚书', english: 'Isaiah', chapters: 66 },
    { id: 'gbJer', name: '耶利米书', english: 'Jeremiah', chapters: 52 },
    { id: 'gbLam', name: '耶利米哀歌', english: 'Lamentations', chapters: 5 },
    { id: 'gbEze', name: '以西结书', english: 'Ezekiel', chapters: 48 },
    { id: 'gbDan', name: '但以理书', english: 'Daniel', chapters: 12 },
    { id: 'gbHos', name: '何西阿书', english: 'Hosea', chapters: 14 },
    { id: 'gbJoe', name: '约珥书', english: 'Joel', chapters: 3 },
    { id: 'gbAmo', name: '阿摩司书', english: 'Amos', chapters: 9 },
    { id: 'gbOba', name: '俄巴底亚书', english: 'Obadiah', chapters: 1 },
    { id: 'gbJon', name: '约拿书', english: 'Jonah', chapters: 4 },
    { id: 'gbMic', name: '弥迦书', english: 'Micah', chapters: 7 },
    { id: 'gbNah', name: '那鸿书', english: 'Nahum', chapters: 3 },
    { id: 'gbHab', name: '哈巴谷书', english: 'Habakkuk', chapters: 3 },
    { id: 'gbZep', name: '西番雅书', english: 'Zephaniah', chapters: 3 },
    { id: 'gbHag', name: '哈该书', english: 'Haggai', chapters: 2 },
    { id: 'gbZec', name: '撒迦利亚', english: 'Zechariah', chapters: 14 },
    { id: 'gbMal', name: '玛拉基书', english: 'Malachi', chapters: 4 }
  ],
  // 新约全书
  newTestament: [
    { id: 'gbMat', name: '马太福音', english: 'Matthew', chapters: 28 },
    { id: 'gbMak', name: '马可福音', english: 'Mark', chapters: 16 },
    { id: 'gbLuk', name: '路加福音', english: 'Luke', chapters: 24 },
    { id: 'gbJhn', name: '约翰福音', english: 'John', chapters: 21 },
    { id: 'gbAct', name: '使徒行传', english: 'Acts', chapters: 28 },
    { id: 'gbRom', name: '罗马书', english: 'Romans', chapters: 16 },
    { id: 'gb1Co', name: '哥林多前书', english: '1 Corinthians', chapters: 16 },
    { id: 'gb2Co', name: '哥林多后书', english: '2 Corinthians', chapters: 13 },
    { id: 'gbGal', name: '加拉太书', english: 'Galatians', chapters: 6 },
    { id: 'gbEph', name: '以弗所书', english: 'Ephesians', chapters: 6 },
    { id: 'gbPhl', name: '腓立比书', english: 'Philippians', chapters: 4 },
    { id: 'gbCol', name: '歌罗西书', english: 'Colossians', chapters: 4 },
    { id: 'gb1Ts', name: '帖撒罗尼迦前书', english: '1 Thessalonians', chapters: 5 },
    { id: 'gb2Ts', name: '帖撒罗尼迦后书', english: '2 Thessalonians', chapters: 3 },
    { id: 'gb1Ti', name: '提摩太前书', english: '1 Timothy', chapters: 6 },
    { id: 'gb2Ti', name: '提摩太后书', english: '2 Timothy', chapters: 4 },
    { id: 'gbTit', name: '提多书', english: 'Titus', chapters: 3 },
    { id: 'gbPhm', name: '腓利门书', english: 'Philemon', chapters: 1 },
    { id: 'gbHeb', name: '希伯来书', english: 'Hebrews', chapters: 13 },
    { id: 'gbJas', name: '雅各书', english: 'James', chapters: 5 },
    { id: 'gb1Pe', name: '彼得前书', english: '1 Peter', chapters: 5 },
    { id: 'gb2Pe', name: '彼得后书', english: '2 Peter', chapters: 3 },
    { id: 'gb1Jn', name: '约翰一书', english: '1 John', chapters: 5 },
    { id: 'gb2Jn', name: '约翰二书', english: '2 John', chapters: 1 },
    { id: 'gb3Jn', name: '约翰三书', english: '3 John', chapters: 1 },
    { id: 'gbJud', name: '犹大书', english: 'Jude', chapters: 1 },
    { id: 'gbRev', name: '启示录', english: 'Revelation', chapters: 22 }
  ]
};

// 提取经文内容的函数
function extractVerses(htmlContent) {
  const verses = [];
  const lines = htmlContent.split('\n');
  
  for (const line of lines) {
    // 匹配 <sup>数字</sup> 开头的经文
    const verseMatch = line.match(/<sup>(\d+)<\/sup>\s*(.+)/);
    if (verseMatch) {
      const verseNumber = parseInt(verseMatch[1]);
      const verseText = verseMatch[2].replace(/<br\s*\/?>/, '').trim();
      if (verseText) {
        verses.push({
          number: verseNumber,
          text: verseText
        });
      }
    }
  }
  
  return verses;
}

// 主函数：提取所有圣经数据
function extractBibleData() {
  const allBooks = [...bibleBooks.oldTestament, ...bibleBooks.newTestament];
  const bibleData = {
    oldTestament: [],
    newTestament: [],
    metadata: {
      totalBooks: allBooks.length,
      oldTestamentBooks: bibleBooks.oldTestament.length,
      newTestamentBooks: bibleBooks.newTestament.length,
      extractedAt: new Date().toISOString()
    }
  };

  // 处理旧约
  for (const book of bibleBooks.oldTestament) {
    console.log(`正在处理旧约: ${book.name}`);
    const bookData = {
      id: book.id,
      name: book.name,
      english: book.english,
      testament: 'old',
      chapters: []
    };

    for (let chapter = 1; chapter <= book.chapters; chapter++) {
      const filePath = path.join(__dirname, book.id, `${book.id}${chapter}.html`);
      
      if (fs.existsSync(filePath)) {
        const htmlContent = fs.readFileSync(filePath, 'utf8');
        const verses = extractVerses(htmlContent);
        
        bookData.chapters.push({
          number: chapter,
          verses: verses
        });
      } else {
        console.warn(`文件不存在: ${filePath}`);
      }
    }

    bibleData.oldTestament.push(bookData);
  }

  // 处理新约
  for (const book of bibleBooks.newTestament) {
    console.log(`正在处理新约: ${book.name}`);
    const bookData = {
      id: book.id,
      name: book.name,
      english: book.english,
      testament: 'new',
      chapters: []
    };

    for (let chapter = 1; chapter <= book.chapters; chapter++) {
      const filePath = path.join(__dirname, book.id, `${book.id}${chapter}.html`);
      
      if (fs.existsSync(filePath)) {
        const htmlContent = fs.readFileSync(filePath, 'utf8');
        const verses = extractVerses(htmlContent);
        
        bookData.chapters.push({
          number: chapter,
          verses: verses
        });
      } else {
        console.warn(`文件不存在: ${filePath}`);
      }
    }

    bibleData.newTestament.push(bookData);
  }

  // 保存到JSON文件
  const outputPath = path.join(__dirname, 'src', 'data', 'bible.json');
  fs.mkdirSync(path.dirname(outputPath), { recursive: true });
  fs.writeFileSync(outputPath, JSON.stringify(bibleData, null, 2), 'utf8');
  
  console.log(`圣经数据已提取完成，保存到: ${outputPath}`);
  console.log(`总共处理了 ${bibleData.oldTestament.length + bibleData.newTestament.length} 本书`);
}

// 运行提取
extractBibleData(); 